package cn.icanci.loopstack.ddk.admin.biz.event.resource;

import cn.icanci.loopstack.ddk.admin.biz.service.AppService;
import cn.icanci.loopstack.ddk.admin.biz.service.NoticeConfigService;
import cn.icanci.loopstack.ddk.admin.biz.service.RegisterService;
import cn.icanci.loopstack.ddk.admin.biz.thread.TriggerThread;
import cn.icanci.loopstack.ddk.common.model.config.AppVO;
import cn.icanci.loopstack.ddk.common.model.config.NoticeConfigVO;
import cn.icanci.loopstack.ddk.common.model.config.RegisterVO;
import cn.icanci.loopstack.ddk.common.socket.publish.PublishMethodInvokeDTO;
import cn.icanci.loopstack.ddk.common.socket.publish.PublishTypeEnum;
import cn.icanci.loopstack.ddk.common.socket.trigger.MethodInvokeTriggerDTO;
import cn.icanci.loopstack.ddk.common.socket.trigger.TriggerDTO;
import cn.icanci.loopstack.lsi.event.BaseEventListener;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;

/**
 * 资源刷新
 * 
 * @author icanci
 * @since 1.0 Created in 2023/01/09 22:41
 */
@Service
public class ResourceEventListener extends BaseEventListener<ResourceEvent> {
    @Resource
    private AppService          appService;
    @Resource
    private NoticeConfigService noticeConfigService;
    @Resource
    private RegisterService     registerService;

    @Override
    protected void event(ResourceEvent event) {
        String resource = event.getResource();
        String value = event.getValue();
        if (StringUtils.isBlank(resource) || StringUtils.isBlank(value)) {
            return;
        }

        List<NoticeConfigVO> noticeConfigList = noticeConfigService.queryByNoticeConfigResource(resource);
        if (CollectionUtils.isEmpty(noticeConfigList)) {
            return;
        }

        // 执行触发器
        TriggerThread.trigger(buildTrigger(noticeConfigList, resource, value));
    }

    /**
     * 构建触发列表
     *
     * @param noticeConfigList noticeConfigList
     * @param resource 资源
     * @param value 资源值
     * @return 返回触发列表
     */
    private List<TriggerDTO> buildTrigger(List<NoticeConfigVO> noticeConfigList, String resource, String value) {
        List<TriggerDTO> methodInvokes = Lists.newArrayList();
        final PublishMethodInvokeDTO invokeDTO = new PublishMethodInvokeDTO(resource, value);
        for (NoticeConfigVO noticeConfigVO : noticeConfigList) {
            MethodInvokeTriggerDTO trigger = new MethodInvokeTriggerDTO();
            trigger.setPublishType(PublishTypeEnum.METHOD_INVOKE);
            trigger.setRegisters(queryRegister(noticeConfigVO.getAppUuid()));
            trigger.setMethodInvoke(invokeDTO);
            methodInvokes.add(trigger);
        }
        return methodInvokes;
    }

    /**
     * 查询注册表
     *
     * @param appUuid appUuid
     * @return 返回注册表
     */
    private List<RegisterVO> queryRegister(String appUuid) {
        AppVO appVO = appService.queryByAppUuid(appUuid);
        if (appVO == null) {
            return Lists.newArrayList();
        }
        return registerService.queryByAppId(appVO.getAppId());
    }
}
